import 'package:flutter/material.dart';
import 'package:flutter_demo/wzw/login.dart';
import '../wzw/login.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyStatefulWidget(),
      debugShowCheckedModeBanner: false,
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return MyState();
  }
}

class MyState extends State {
  var data = "default";

  //采用async/await解决多层嵌套的问题
  task() async {
    String a = await Future.delayed(new Duration(seconds: 2), () {
      return "hello";
    });
    String b = await Future.delayed(new Duration(seconds: 2), () {
      return "world";
    });
    String c = await Future.delayed(new Duration(seconds: 2), () {
      return "!!!";
    });

    await Future.delayed(Duration(seconds: 1),(){
      setState(() {
        return "$a$b$c";
      });
    });



  }

  @override
  void initState() {
    super.initState();
    // 不合理的嵌套
    Future.delayed(new Duration(seconds: 2), () {
      return "hello";
    }).then((value) {
      Future.delayed(new Duration(seconds: 2), () {
        return value + "world";
      }).then((value) {
        Future.delayed(new Duration(seconds: 3), () {
          return value + "!";
        }).then((value) {
          setState(() {
            data = value;
          });
        }, onError: (e) {
          print(e);
        });
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
          child: new GestureDetector(
        child: Text(
          "$data 捕捉精彩，分享快乐",
          style: TextStyle(color: Color(0xff006fff), fontSize: 18),
        ),
        onTap: () async{
          var result = await Navigator.of(context).pushReplacement(new MaterialPageRoute(builder: (context){
            return new LoginPageStatelessWidget();
          }));

          setState(() {
            data = result;
          });
        },
      )),
    );
  }
}
